<template>
	<view class="container">
		<view class="content">
			<view class="list" v-for="(item,index) in addressList" :key="index" @click="selectClick(index)">
				<view class="select">
					<u-icon name="checkmark-circle-fill" color="#ff3b3b" size="40" v-if="item.select == true"></u-icon>
					<view class="no" v-else></view>
				</view>
				<view class="infos">
					<view class="user_info">{{item.username+' '+item.telephone}}
						<view v-if="item.is_def == 1">默认</view>
					</view>
					<view class="address">{{item.province + item.city + item.county + item.adrdetail}}</view>
				</view>
				<view style="height: 80rpx;width: 80rpx;display: flex;align-items: center;justify-content: center;"
					@click.stop="$gourl('/pages/address/editAddress?id='+item.address_id)">
					<u-icon name="edit-pen" color="#999" size="40"></u-icon>
				</view>
			</view>
		</view>
		<view class="add_btn" @click="$gourl('/pages/address/addAddress')">
			<u-icon name="plus-circle" color="#ff3b3b" size="40"></u-icon>
			<text style="margin-left: 20rpx;">新增地址</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				selectId: null
			}
		},
		onShow() {
			if (uni.getStorageSync('selectAddressId')) {
				this.selectId = uni.getStorageSync('selectAddressId')
			}
			this.getData()
		},
		methods: {
			async getData() {
				uni.showLoading({
					title: '加载中...'
				})
				await this.$allApi.addressList().then(res => {
					this.addressList = res.data.list
					for (let item of this.addressList) {
						if (this.selectId && item.address_id == this.selectId) {
							item.select = true
						} else {
							item.select = false
						}
						uni.hideLoading()
					}
				})
			},
			selectClick(index) {
				for (let i = 0; i < this.addressList.length; i++) {
					if (i == index) {
						this.addressList[i].select = true
						uni.setStorageSync('selectAddressId', this.addressList[i].address_id)
						getApp().selectAddress = JSON.stringify(this.addressList[i])
					} else {
						this.addressList[i].select = false
					}
				}
				this.$forceUpdate()
				this.$backTo(1)
			}
		}
	}
</script>

<style scoped lang="scss">
	.no {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: 1px solid #ccc;
	}

	.container {
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	.list {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		border-bottom: 1px solid #eee;

		.infos {
			flex: 1;
			padding: 0 20rpx;

			.user_info {
				font-size: 30rpx;
				font-weight: 400;
				display: flex;
				justify-content: flex-start;
				align-items: center;

				view {
					width: 74rpx;
					height: 30rpx;
					border-radius: 30rpx;
					margin-left: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 20rpx;
					color: #e2b771;
					background: linear-gradient(to right, #585858, #333);
					;
				}
			}

			.address {
				font-size: 24rpx;
			}
		}
	}

	.add_btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		height: 120rpx;
		padding-bottom: calc(-30rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		border-top: 1px solid #eee;
	}
</style>
